package com.iiitb.wtp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Map;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
 
 
public class URLConnect {
		
	URLConnect(Map<String, String> urlList) {
		
		Parser ObjParse = new Parser();
		
		//for(int i=0; i<urlList.size(); i++){
		for (Map.Entry<String, String> entry : urlList.entrySet()) {
			String url = entry.getKey();
			//String url = urlList.
			try {
				HttpHost proxy = new HttpHost("192.168.3.254", 8080, "http");
				HttpClient client = new DefaultHttpClient();
				client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
	    		HttpGet get = new HttpGet(url);
	    		
	    		HttpResponse response = client.execute(get);
	    		InputStreamReader is = new InputStreamReader(response.getEntity().getContent(),"UTF8");
	    		BufferedReader rd = new BufferedReader(is);
	    		
	    		String line;
	    		String sourceLine = null ;
	    		
	    		while ((line = rd.readLine()) != null) {
	    			sourceLine = sourceLine + line;
	    		}
	           
	            //Call the parser from here
	            urlList = ObjParse.parse(sourceLine, url, entry.getValue(), urlList);
	            rd.close();	            
	            
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
		}
    }
}